package com.example.myanimator.shadowLayer;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.BlurMaskFilter;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PathMeasure;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

import androidx.annotation.Nullable;

import com.example.myanimator.R;

/**
 * @author tgw
 * @date 2021/3/30
 * @describe  文本阴影
 */
public class DrawBitmapShadow extends View {

    private Path mCirclePath, mDstPath, linePath, mLineDstPath;
    private Paint mPaint;
    private Bitmap mBitmap;

    public DrawBitmapShadow(Context context) {
        super(context);
    }

    public DrawBitmapShadow(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public DrawBitmapShadow(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    private void init() {
        setLayerType(LAYER_TYPE_SOFTWARE, null);
        mBitmap = BitmapFactory.decodeResource(getResources(), R.mipmap.aa);

        mPaint = new Paint();
        mPaint.setAntiAlias(true); // 去除锯齿
        mPaint.setDither(true); // 抗抖动
        mPaint.setStrokeWidth(2);
        mPaint.setTextSize(100);//设置文字大小
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setColor(Color.BLACK);
        //radius 为阴影模糊半径
        mPaint.setShadowLayer(150, 10, 10, Color.GRAY);
    }

    @Override
    protected void onDraw(Canvas canvas) {

        canvas.drawColor(Color.WHITE);
        mPaint.setColor(Color.BLACK);
        canvas.drawText("文本阴影与setMaskFilter的使用", 100, 100, mPaint);
        canvas.drawCircle(300, 100, 50, mPaint);


        canvas.drawBitmap(mBitmap, null, new Rect(500, 50, 500 + mBitmap.getWidth(),
                50 + mBitmap.getHeight()), mPaint);



        //setMaskFilter()  该函数 不支持 硬件加速
        /**
         * Blur sty ：发光样式，有 Blur.INNER 内发光〕、 Blur.SOLID （外发光）、 Blur.NORMAL
         * （内外发光〉、  Blur.OUTER仅显示发光效果） 样式
         * */

        mPaint.setMaskFilter(new BlurMaskFilter(50, BlurMaskFilter.Blur.INNER));
        mPaint.setColor(Color.RED);
        mPaint.setStyle(Paint.Style.FILL);
        canvas.drawCircle(300, 300, 50, mPaint);

    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {

        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                mPaint.clearShadowLayer();
                break;
            case MotionEvent.ACTION_MOVE:

                break;
            case MotionEvent.ACTION_UP:
                mPaint.setShadowLayer(150, 10, 150, Color.GRAY);
                break;
        }

        invalidate();
        return true;
    }


}
